/*
 * Copyright (C) 2012-2016 TeraCN
 * 
 * This file is part of TeraCN.
 * 
 * TeraCN is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * TeraCN is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package tera.gameserver.network.serverpackets;

import java.nio.ByteBuffer;

import tera.gameserver.model.npc.Npc;
import tera.gameserver.model.playable.Player;
import tera.gameserver.network.ServerPacketType;
import tera.gameserver.templates.NpcTemplate;

/**
 * @author Ronn Mod Evestu
 */
public class NpcInfo extends ServerPacket
{
	private static final ServerPacket instance = new NpcInfo();
	
	/**
	 * Method getInstance.
	 * @param npc Npc
	 * @param player Player
	 * @return NpcInfo
	 */
	public static NpcInfo getInstance(Npc npc, Player player)
	{
		final NpcInfo packet = (NpcInfo) instance.newInstance();
		packet.objectId = npc.getObjectId();
		packet.subId = npc.getSubId();
		packet.x = npc.getX();
		packet.y = npc.getY();
		packet.z = npc.getZ();
		final NpcTemplate template = npc.getTemplate();
		packet.heading = npc.getHeading();
		packet.npcId = template.getIconId();
		packet.npcType = npc.getTemplateType();
		packet.spawned = npc.isSpawned() ? 1 : 0;
		packet.isFriend = npc.isFriend(player) ? 1 : 0;
		return packet;
	}
	
	private int objectId;
	private int subId;
	private float x;
	private float y;
	private float z;
	private int heading;
	private int npcId;
	private int npcType;
	private int isFriend;
	private int spawned;
	
	/**
	 * Method getPacketType.
	 * @return ServerPacketType
	 */
	@Override
	public ServerPacketType getPacketType()
	{
		return ServerPacketType.S_SPAWN_NPC;
	}
	
	/**
	 * Method isSynchronized.
	 * @return boolean
	 * @see rlib.network.packets.SendablePacket#isSynchronized()
	 */
	@Override
	public boolean isSynchronized()
	{
		return false;
	}
	
	/**
	 * Method writeImpl.
	 * @param buffer ByteBuffer
	 */
	@Override
	protected void writeImpl(ByteBuffer buffer)
	{
		writeOpcode(buffer);
		writeInt(buffer, 0);
		final int shiftreplace = buffer.position();
		writeShort(buffer, 109);
		writeInt(buffer, objectId);
		writeInt(buffer, subId);
		writeLong(buffer, 0);
		writeFloat(buffer, x);
		writeFloat(buffer, y);
		writeFloat(buffer, z);
		writeShort(buffer, heading);
		writeInt(buffer, 12);
		writeInt(buffer, npcId);
		writeShort(buffer, npcType);
		writeShort(buffer, 0);
		writeInt(buffer, 110);
		writeShort(buffer, 0);
		writeInt(buffer, 5);
		writeByte(buffer, 1);
		writeByte(buffer, isFriend);
		writeShort(buffer, spawned);
		writeLong(buffer, 0);
		writeLong(buffer, 0);
		writeLong(buffer, 0);
		writeLong(buffer, 0);
		writeLong(buffer, 0);
		writeShort(buffer, 0);
		writeByte(buffer, 0);
		writeInt(buffer, 0);
		writeByte(buffer, 0);
		writeInt(buffer, 0xB9ACC5D8);
		writeInt(buffer, 0x0000CE74);
		final int shift = buffer.position();
		buffer.position(shiftreplace);
		writeShort(buffer, shift - 8);
		buffer.position(shift);
	}
}